// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    windowHeight: wx.getSystemInfoSync().windowHeight,
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName'), // 如需尝试获取用户信息可改为false
    showButton: false,
    diaShow: 'none',
    name: '',
    telephone: '',
    password: '',
    passwordAgain: '',
    token: wx.getStorageSync('token') || ''
  },
  onLoad() {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true,
        code: wx.getStorageSync('code')
      })
    }
    this.setData({
      token: wx.getStorageSync('token') || ''
    })
  },
  onShow: function () {
    let that = this
    app.fadein(that, 'current3', 0)
    setTimeout(function () {
      app.fadein(that, 'current3', 1)
    }, 1000) 
    this.getIdentity();
  },
  getIdentity(){
    let that = this;
    wx.showLoading({
      title: '加载中',
    });
    let code = that.data.code
    wx.login({
      success: res => {
        code = res.code
        wx.request({
          url: 'https://test.cloudcampusapps.top:8080'+'/weChatWifiLogin/queryWeiUser'+'?flag=1'+'&code='+code+'&nickName='+wx.getStorageSync('userInfo').nickName,//请求的url
          header: {
            "Content-Type": "application/json",
            "token": that.data.token
          },
          method: "GET",
          success:function(res){
          if(res.data.data.code === 10001) {  //  未注册
            that.setData({
              showButton: true
            })
          } else {
              wx.setStorageSync('token', res.data.data.Token)
              that.setData({
                showButton: false
              })
              setTimeout(function () {
                wx.navigateTo({
                  url: '../shopList/shopList?customerOrBossId='+res.data.data.bossId
                });
              }, 2000)
            }
            wx.hideLoading();
          },
          fail:function(err){
            console.log('err=============='+err)
            wx.hideLoading()
          }
        })
      }
    })
    
  },
  registered() {
    this.setData({
      diaShow: 'block'
    });
  },
  formSubmit(e) {
    let item =  e.detail.value;
    let reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
    // if( !(item.name && item.telephpne && item.password && item.passwordAgain) ) {
    //   wx.showToast({
    //     title: '请输入信息',
    //     icon: 'none',
    //     duration: 2000
    //   })
    // }else if(item.password!=item.passwordAgain) {
    //   wx.showToast({
    //     title: '两次密码不一致，请确认您的密码',
    //     icon: 'none',
    //     duration: 2000
    //   })
    // } else
    if(!reg_tel.test(item.telephpne)) {
      wx.showToast({
        title: '请重新校验您的手机号',
        icon: 'none',
        duration: 2000
      })
    } else {
      this.setData({
        telephpne: item.telephpne
      });
      this.getRegister();
    }
  },
  noCallback() {
    this.setData({
      diaShow: 'none',
      telephpne: ''
    });
  },
  getRegister() {
    this.setData({
      diaShow: 'none'
    });
    let that = this;
    wx.login({
      success: res => {
        let params={
          "code": res.code,
          "phone": that.data.telephpne,
          "flag": 1,
          "nickName":  wx.getStorageSync('userInfo').nickName
        };
        wx.showLoading({
          title: '加载中',
        });
        wx.request({
          url: 'https://test.cloudcampusapps.top:8080/weChatWifiLogin/addWeiUser',
          header: {
            "Content-Type": "application/json",
            "token": ''
          },
          method: "POST",
          data: JSON.stringify(params),
          success:function(res){
            if(res.data.code==200){
              wx.setStorageSync('token', res.data.data.Token)
              wx.navigateTo({
                url: '../shopList/shopList?customerOrBossId='+res.data.data.bossId
              });
            } else {
              wx.showToast({
                title: res.data.errorMsg,
                icon: 'none',
                duration: 2000
              })
            }
            wx.hideLoading();
          },
          fail:function(err){
            console.log('err=============='+err);
            wx.hideLoading();
          }
        })
      }
    })
    
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log('res',res)
        wx.setStorageSync('userInfo', res.userInfo); 
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log('e',e)
    wx.setStorageSync('userInfo',  e.detail.userInfo); 
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
